﻿using FighttheLandOwner.Net;
using FighttheLandOwner.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FighttheLandOwner
{
    public partial class Frm_Log : Form
    {
        public Frm_Log()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }
        bool connected = false;
        private void btn_log_Click(object sender, EventArgs e)
        {
            General.Client.Connect();
            General.Client.ServerConnected += Client_ServerConnected;
            General.Client.PlaintextReceived += Client_PlaintextReceived;
            while (!connected) ;
            General.Client.Send("Log|" + tbx_name.Text);
        }

        private void Client_PlaintextReceived(object sender, AsyncTcp.Client.TcpDatagramReceivedEventArgs<string> e)
        {
            if (e.Datagram == "ok")
            {
                Hide();
                General.Client.PlaintextReceived -= Client_PlaintextReceived;
                Thread thread = new Thread(delegate ()
                  {
                      new Frm_Game(tbx_name.Text).ShowDialog();
                  });
                thread.Start();
            }
            else
            {
                MessageBox.Show("玩家名存在");
            }
        }

        private void Client_ServerConnected(object sender, AsyncTcp.Client.TcpServerConnectedEventArgs e)
        {
            connected = true;
        }
        
    }
}
